package cm.mefobe.android.telecomcost.view;

import java.util.ArrayList;

import cm.mefobe.android.telecomcost.adapter.ProviderSpinnerAdapter;
import cm.mefobe.android.telecomcost.entities.Provider;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class ProviderSpinner extends Spinner {

	private ArrayList<Provider> _providers;
	private Context _context;
	private SpinnerAdapter _adapter;
	private CountrySpinner _countrySpinner;
	private int _position;
	private boolean settingSelection;
	private Provider _provider;

	@Override
	public void setSelection(int position) {
		Log.i("ProviderSpinner", "setting selection..." + position);
		_provider = _providers.get(position);
		setPosition(position);
		settingSelection = true;
		super.setSelection(position);
	}

	public ArrayList<Provider> getProviders() {
		return _providers;
	}

	public Provider getSelectedProvider() {
		return _provider.getId() != -1 ? _provider : null;
	}

	private void setList() {
		if (_context != null) {
			if (_countrySpinner != null) {
				if (_countrySpinner.getSelectedCountry() != null) {
					Log.i("ProviderSpinner", "binding providers...");
					_providers = _countrySpinner.getSelectedCountry()
							.getProviders();					
				} else {
					Log.i("ProviderSpinner", "no countries...");
					_providers = new ArrayList<Provider>();					
				}
				
				_providers.add(0,Provider.allProvider());
				_adapter = new ProviderSpinnerAdapter(_context,
						android.R.layout.select_dialog_singlechoice,
						_providers);
				
			} else {
				Log.i("ProviderSpinner", "No CountrySpinner selected");
			}
		} else {
			Log.i("ProviderSpinner",
					"cannot set the adapter of the ProviderSpinner...the context is null");
		}
	}

	@Override
	public void setAdapter(SpinnerAdapter adapter) {
		Log.i("ProviderSpinner", "setting the adapter...");
		if (settingSelection) {
			Log.i("ProviderSpinner",
					"setting selection...we don't need to set list");
			settingSelection = false;
		} else {
			setList();
			if (_adapter != null) {
				super.setAdapter(_adapter);
				_provider = _adapter.getCount() > 0 ? _providers.get(0) : null;
			} else {
				Log.i("ProviderSpinner", "_adapter is null...");
			}
			
			settingSelection = false;
		}
	}

	@Override
	public SpinnerAdapter getAdapter() {
		return _adapter;
	}

	public ProviderSpinner(Context context, CountrySpinner CountrySpinner) {
		super(context);
		_context = context;
		_countrySpinner = CountrySpinner;
	}

	public CountrySpinner getCountrySpinner() {
		return _countrySpinner;
	}

	public void setCountrySpinner(CountrySpinner countrySpinner) {
		if (countrySpinner != null) {
			this._countrySpinner = countrySpinner;
			_countrySpinner
					.setOnItemSelectedListener(new OnItemSelectedListener() {

						public void onItemSelected(AdapterView<?> arg0,
								View arg1, int arg2, long arg3) {
							setAdapter(null);
						}

						public void onNothingSelected(AdapterView<?> arg0) {
							Log.i("ProviderSpinner", "onNothingSelected");
							setAdapter(null);
						}
					});
		}
	}

	public ProviderSpinner(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		_context = context;
	}

	public ProviderSpinner(Context context, AttributeSet attrs) {
		super(context, attrs);
		_context = context;
	}

	public ProviderSpinner(Context context) {
		super(context);
		_context = context;
	}

	public void setSelectedProvider(Provider Provider) {
		this._provider = Provider;
	}

	public void setPosition(int _position) {
		this._position = _position;
	}

	public int getPosition() {
		return _position;
	}
}
